package jh.com.jhappbf.ui;

import android.app.Application;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ab.activity.AbActivity;
import com.ab.util.AbToastUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.configs.Constant;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.security.DES;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class UpdatePwd extends AbActivity {

    private AbTitleBar mAbTitleBar;
    private EditText mTxtOldPassword;
    private EditText mTxtNewPassword;
    private EditText mTxtConfirmPassword;
    private Button mUpdateBtn;
    private String mAccount;
    private Application mApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.update_pwd_activity);
        mAccount = State.mAccount;
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText(mAccount);
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(20, 0, 0, 0);
        mAbTitleBar.setGravity(Gravity.CENTER_HORIZONTAL);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mApplication = getApplication();
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        mTxtOldPassword = findViewById(R.id.updatePwd_old_pwd_txt);
        mTxtNewPassword = findViewById(R.id.updatePwd_new_pwd_txt);
        mTxtConfirmPassword = findViewById(R.id.updatePwd_confirm_pwd_txt);
        mUpdateBtn = findViewById(R.id.update_pwd_btn);
        mUpdateBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                updatePwd();
            }
        });
    }

    /**
     * 修改密码
     */
    private void updatePwd() {
        mUpdateBtn.setEnabled(false);
        String oldPwd = mTxtOldPassword.getText().toString();
        String newPwd = mTxtNewPassword.getText().toString();
        String confirmPwd = mTxtConfirmPassword.getText().toString();
        if (mAccount.length() == 0) {
            AbToastUtil.showToast(UpdatePwd.this, "密码对应的账号不能为空!");
            return;
        }

        if (oldPwd.length() == 0) {
            AbToastUtil.showToast(UpdatePwd.this, "旧密码不能为空!");
            return;
        }

        if (newPwd.length() == 0) {
            AbToastUtil.showToast(UpdatePwd.this, "新密码不能为空!");
            return;
        }

        if (!newPwd.equals(confirmPwd)) {
            AbToastUtil.showToast(UpdatePwd.this, "两次密码不一致!");
            return;
        }

        String methodName = "updateUserPwd";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("account");
        param.add("oldPwd");
        param.add("newPwd");
        value.add(mAccount);
        value.add(DES.encrypt(oldPwd, Constant.DESKEY));
        value.add(DES.encrypt(newPwd, Constant.DESKEY));
        Listener<String> listener = response -> {
            mUpdateBtn.setEnabled(true);
            if (response != null) {
                JSONObject obj = JsonHelper.getJsonObject(response);
                String result = obj != null ? obj.optString("result") : null;
                if ("true".equals(result)) {
                    AbToastUtil.showToast(mApplication, "修改成功！");
                } else {
                    AbToastUtil.showToast(mApplication, "修改失败！");
                }
            }
        };

        ErrorListener errorListener = error -> {
            mUpdateBtn.setEnabled(true);
            AbToastUtil.showToast(mApplication, "修改失败，发生异常！");
        };
        VolleyCallWebService request = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton volleySingleton = VolleySingleton.getVolleyInstance(getApplicationContext());
        volleySingleton.addRequest(request);
    }
}
